package SecondeStage.XianCheng;

public class ThreadMethod02 {
    public static void main(String[] args) throws InterruptedException {
        T2 t2 = new T2();
        t2.start();

        for (int i = 0; i <=20 ; i++) {
            Thread.sleep(1000);
            System.out.println("主线程吃了"+i +" 包子");
            if(i==5){
                // 线程插队,一旦插入成功，则先执行完插入的线程的所有任务
                t2.join();
                // 线程礼让，不一定成功
                //Thread.yield();
            }
        }
    }
}

class T2 extends Thread{
    @Override
    public void run() {
        for (int i = 0; i <= 20; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("子线程吃了"+i +" 包子");
        }

    }
}
